﻿
function chkFm() {
    var patrn = /^[\-\+]?([0-9]\d*|0|[1-9]\d{0,2}(,\d{3})*)(\.\d+)?$/;
    if ($("#drp_company").val() == "") {
        $("#val_company").show();
        return false;
    }
    //            if ($("#txt_taxfee").val() == "") {
    //                $("#val_taxfee").show();
    //                return false;
    //            }

    //            if (!patrn.exec($("#txt_taxfee").val())) {
    //                $("#val_taxfee").html("格式不正确！");
    //                $("#val_taxfee").show();
    //                return false;
    //            }

    //            if ($("#txt_fireightFee").val() == "") {
    //                $("#val_fireightFee").show();
    //                return false;
    //            }

    //            if (!patrn.exec($("#txt_fireightFee").val())) {
    //                $("#val_fireightFee").html("格式不正确！");
    //                $("#val_fireightFee").show();
    //                return false;
    //            }
    if ($("#hd_proScope").val() == "") {
        $("#val_proScope").show();
        return false;
    }

    if ($("#txt_Amount").val() == "") {
        $("#val_Amount").show();
        return false;
    }
    if (!patrn.exec($("#txt_Amount").val())) {
        $("#val_Amount").html("格式不正确！");
        $("#val_Amount").show();
        return false;
    }
    return true;
}

function chkCompany() {
    if ($("#drp_company").val() == "") {
        $("#val_company").show();
        return false;
    }
    //            if ($("#txt_taxfee").val() == "") {
    //                $("#val_taxfee").show();
    //                return false;
    //            }
    //            if ($("#txt_fireightFee").val() == "") {
    //                $("#val_fireightFee").show();
    //                return false;
    //            }
    return true;
}

function checkCountNum(obj) {
    if (obj.value != "" && obj.value != "0") {
        var flag = IsPriceNumeric(obj.value);
        if (!flag)
            obj.value = "1";
    }
    else
        obj.value = "1";
}
function IsPriceNumeric(oNum) {
    var strP = /^[0-9]+([.]\d{1,2})?$/;
    if (strP.test(oNum))
        return true;
    else
        return false;
}




function resetSrh() {
    document.getElementById("txt_ProductNo").value = "";
    document.getElementById("txt_ProductName").value = "";
    $('#ddl_CategoryRoot').val("");
    $('#ddl_CategorySub').val("");
}

function selectAll(obj) {
    if ($(obj).attr("checked") == true) { // 全选 
        $(":checkbox").each(function() { //可以对.net服务器控件有效 
            $(this).attr("checked", true);
        });
    } else { // 取消全选 
        $(":checkbox").each(function() { //可以对.net服务器控件有效
            $(this).attr("checked", false);
        });
    }
}
function checkSelect() {
    var cbo = 0;
    $(":checkbox").each(function() {
        if (this.checked) {
            cbo++;
        }
    });
    if (cbo == 0) {
        alert("请选择商品！");
        return false;
    }
    else
        return true;
}

function delFromHiddenValue(productId) {
    var products = [];
    if ($('#hd_proScope').val().length > 0) {
        products = jsonParse($('#hd_proScope').val());

        for (var i = 0; i < products.length; i++) {
            if (products[i]["productId"] == productId) {
                products.remove(i);
                break;
            }
        }

        $('#hd_proScope').val(Object.serialize2Str(products));

        updateTableRow();
    }
}
function updateTableRow() {

    var products = [];
    if ($('#hd_proScope').val().length > 0) {
        products = jsonParse($('#hd_proScope').val());

        var sw = [];

        var totalAmount = 0;

        for (var i = 0; i < products.length; i++) {
            sw.push('<tr align="center" style="height: 20px;">');
            sw.push('<td bgcolor="#FFFFFF">' + (i + 1) + '</td>');
            sw.push('<td bgcolor="#FFFFFF"><a target="_blank" href="../user_productDetail_' + products[i]['productId'] + '.html">' + products[i]['productName'] + '</a></td>');
            sw.push('<td bgcolor="#FFFFFF"><span class="red">' + products[i]['price'] + '</span></td>');
            sw.push('<td bgcolor="#FFFFFF">' + GetbusinessNature(products[i]['businessNature']) + '</td>');
            sw.push('<td bgcolor="#FFFFFF">' + products[i]['quantity'] + '</td>');
            sw.push('<td bgcolor="#FFFFFF">' + products[i]['note'] + '</td>');
            var amount = accMultiply(products[i]['quantity'], products[i]['price']);
            totalAmount = accPlus(totalAmount, amount);
            sw.push('<td bgcolor="#FFFFFF">' + amount + '</td>');
            sw.push('<td bgcolor="#FFFFFF"><a href="javascript:void(0);" onclick="delFromHiddenValue(' + products[i]['productId'] + ');">删除</a></td>');
            sw.push('</tr>');
        }

        totalAmount = totalAmount.toFixed(2);

        $('#hd_TotalAmount').val(totalAmount);
        $('#span_proSum').text(totalAmount);


        $('#lbl_StandardAmount').text(totalAmount);
        $('#txt_Amount').val(totalAmount);

        $('#tableBody').empty();
        $('#tableBody').append(sw.join(''));

    }
}

function addToOrderItem(obj, productId, unit, price, businessnature) {

    var products = [];
    if ($('#hd_proScope').val().length > 0) {
        products = jsonParse($('#hd_proScope').val());
    }

    var isExists = false;

    $(products).each(function(entryIndex, entry) {
        if (entry['productId'] == productId && entry['businessNature'] == businessnature) {
            isExists = true;
            return false;
        }
    });

    var productName = $.trim($(obj).parent().prev().prev().prev().prev().prev().text());
    var quantity = $.trim($($(obj).parent().prev().prev().find('input')[0]).val());
    var note = $($(obj).parent().prev().find('input')[0]).val(); ;

    if (!isExists) {
        var product = { 'productId': productId, 'unit': unit, 'productName': productName, 'price': price, 'quantity': quantity, 'note': note, 'businessNature': businessnature };
        products.push(product);
        $('#hd_proScope').val(Object.serialize2Str(products));
        updateTableRow();
    }
    else {
        alert('您选择的商品已经存在！');
        return false;
    }
}

function GetbusinessNature(nature) {
    var temp;
    if (nature != "") {
        switch (parseInt(nature)) {
            case 1: temp = "周报价"; break;
            case 2: temp = "餐饮"; break;
            case 3: temp = "食堂"; break;
            case 4: temp = "酒店"; break;
        }
    }
    return temp;
}